package com.my.mail2.loadblanch;

import com.my.mail2.service.impl.AbstractEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 链接邮件服务
 */
@Component
public class EmailServiceHandler {

    @Autowired
    private List<AbstractEmailService> emailServiceChain;

    @PostConstruct
    private void init() {
        if (emailServiceChain.size() < 2) {
            return;
        }

        AbstractEmailService current = null;
        for (AbstractEmailService s : emailServiceChain) {
            if (current != null) {
                current.setNextService(s);
            }
            current = s;
        }
        current.setNextService(emailServiceChain.get(0));
    }

}
